﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils.DatabaseUtils;
using System.Data;
using Utils.DC.Web.UI;
using Utils;

namespace DCSLibrary
{
    public class AdvUI
    {
        private const String CACHE_KEY = "Adv_GetAll";
        public int Add(int CategoryPageID, string CompanyName, string Location, string AdvSource, 
            string AdvDescription, string AdvLink, int ViewOrder, string ClickType, string AdvType, bool IsApproved,
            string MetaKeyword, string MetaDescription )
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Adv_add", CategoryPageID, CompanyName, Location, AdvSource,
                                        AdvDescription, AdvLink, ViewOrder,ClickType, AdvType, IsApproved, MetaKeyword, MetaDescription);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int AdvID, int CategoryPageID, string CompanyName, string Location, string AdvSource,
            string AdvDescription, string AdvLink, int ViewOrder, string ClickType, string AdvType, bool IsApproved,
            string MetaKeyword, string MetaDescription)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Adv_update", AdvID, CategoryPageID, CompanyName, Location, AdvSource,
                                        AdvDescription, AdvLink, ViewOrder,
                                        ClickType, AdvType, IsApproved, MetaKeyword, MetaDescription);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + AdvID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int UpdateOrder(String listId, String listOrder)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Adv_UpdateOrder", listId, listOrder);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "Adv_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int AdvID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + AdvID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "Adv_get", AdvID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + AdvID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int AdvID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Adv_delete", AdvID);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
